package com.yun.zhong.fu.sharecenter.Interceptor;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author RenPu
 * @Date 2021/1/5 18:31
 * @Version 1.0
 * @Description: 实现token可以通过feign的传递编写全局，通过@RequestHeader注解来实现，假如接口较多的时候会比较繁琐
 **/
public class TokenReplyInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //1：获取token；
        //获取request对象（spring mvc获取request对象的方式）
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes requestAttributes1 = (ServletRequestAttributes) requestAttributes;
        HttpServletRequest request = requestAttributes1.getRequest();
        //获取token值
        String token = request.getHeader("X-Token");

        //2：将 token进行传递：
        if (StringUtils.isNotBlank(token)) {
            requestTemplate.header("X-Token", token);

        }

    }
}
